package com.xzit.headline.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.joanzapata.android.BaseAdapterHelper;
import com.joanzapata.android.QuickAdapter;
import com.xzit.headline.R;
import com.xzit.headline.bean.PicData;
import com.xzit.headline.net.MyApi;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PicFragment extends BaseFragment {
    //1. 布局

    //变量
    private boolean isListVisible = true;
    private ListView listView;
    private GridView gridView;

    @Override
    public View getMyView() {
        View view = View.inflate(getContext(), R.layout.fragment_pic, null);

        //初始化组件
        final ImageView switchBtn = view.findViewById(R.id.switchBtn);
        listView = view.findViewById(R.id.listView);
        gridView = view.findViewById(R.id.gridView);

        switchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isListVisible = !isListVisible;
                if (isListVisible) {
                    switchBtn.getResources();
                    listView.setVisibility(View.VISIBLE);
                    gridView.setVisibility(View.GONE);
                } else {
                    listView.setVisibility(View.GONE);
                    gridView.setVisibility(View.VISIBLE);
                }
            }
        });

        return view;
    }

    //2.取服务端的数据
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //retrofit
        retrofit.create(MyApi.class).getPicData().enqueue(new Callback<PicData>() {
            @Override
            public void onResponse(Call<PicData> call, Response<PicData> response) {
                //成功
                Toast.makeText(getContext(), response.body().data.title, Toast.LENGTH_SHORT).show();
                setDataToView(response.body().data.news);
            }

            @Override
            public void onFailure(Call<PicData> call, Throwable t) {
                //失败
                Toast.makeText(getContext(), "失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 组图的构造器
     */
    class PicAdapter extends QuickAdapter<PicData.PicDataBean.PicNewsBean> {
        public PicAdapter(Context context, int layoutResId, List<PicData.PicDataBean.PicNewsBean> data) {
            super(context, layoutResId, data);//参数一:上下文  参数二:布局文件  参数三:集合
        }

        @Override
        protected void convert(BaseAdapterHelper helper, PicData.PicDataBean.PicNewsBean item) {
            //赋值,不需要写if判断,也不需要做holder
            helper.setText(R.id.item_title, item.title);//id , 数据
            ImageView imageView = helper.getView(R.id.item_image);
            String url = item.listimage.replace("http://10.0.2.2:8080/jrtt/", BASEURL);
            Glide.with(getContext()).load(url).into(imageView);

            //设置图片的缩放比例
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }
    }

    private void setDataToView(List<PicData.PicDataBean.PicNewsBean> news) {
        //3.将数据设置给布局
        //3.1 布局条目
        //3.2 编写QuickAdapter
        PicAdapter adapter = new PicAdapter(getContext(), R.layout.item_list_pic, news);
        listView.setAdapter(adapter);
        gridView.setAdapter(adapter);
    }


}
